﻿using AnesSystem.Models;
using AnesSystem.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls.ShuQian.PaiTai.CDTY
{

    /// <summary>
    /// SecheduledListCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class SecheduledListCtrl : UserControl
    {
        #region Variables

        IList checkedList = new List<PatientLinkModel>();

        #endregion

        #region Constructor
        public SecheduledListCtrl()
        {
            InitializeComponent();
            this.DataContext = SecheduledListCtrlViewModel.Instance;

        }

        #endregion

        #region Properties

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void PaiBan(IList list)
        {
            try
            {
                DataObject dataObject = null;
                IList tempList = new List<PatientLinkModel>();
                foreach (PatientLinkModel item in list)
                {
                    if (item.Application.Detailstatus.Equals(Constants.OR_STATUS_FLAG_APPLIED) || item.Application.Detailstatus.Equals(Constants.OR_STATUS_FLAG_SCHEDULED))
                    {
                        tempList.Add(item);
                    }
                }
                if (tempList.Count != 0)
                {
                    dataObject = new DataObject("2", tempList);
                }
                //else if (bingRenList1.SelectedItem != null || bingRenList1.SelectedValue != null)
                //{
                //    PatientLinkModel p = bingRenList1.SelectedItems[0] as PatientLinkModel;
                //    if (p.Application.Detailstatus.Equals(Constants.OR_STATUS_FLAG_APPLIED) || p.Application.Detailstatus.Equals(Constants.OR_STATUS_FLAG_SCHEDULED))
                //        dataObject = new DataObject("2", new List<PatientLinkModel>() { bingRenList1.SelectedItems[0] as PatientLinkModel });
                //}
                if (dataObject == null) return;

                System.Windows.DragDrop.DoDragDrop(bingRenList1, dataObject, DragDropEffects.Copy);
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        private void del_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void del_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MessageBoxCommon.ShowDialog("确定要撤销此病人?", MessageBoxCommon.OKCANCLE).Value == true)
                {
                    PatientLinkModel patientLinkModel = (sender as Button).DataContext as PatientLinkModel;
                    SecheduleMainCtrlViewModel.Instance.DeleteSecheduled(patientLinkModel);
                    SecheduleMainCtrlViewModel.Instance.RefUI();
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void CustomComBoxTaiShuCtrl_GotFocus(object sender, RoutedEventArgs e)
        {
            SecheduledListCtrlViewModel.Instance.SelectedP = (sender as CustomComBoxTaiShuCtrl).Tag as PatientLinkModel;
        }

        private void DoctorCtrl_GotFocus(object sender, RoutedEventArgs e)
        {
            SecheduledListCtrlViewModel.Instance.SelectedP = (sender as DoctorCtrl).Tag as PatientLinkModel;
        }
        private void AnesDoctorCtrl_GotFocus(object sender, RoutedEventArgs e)
        {
            SecheduledListCtrlViewModel.Instance.SelectedP = (sender as MaZuiDoctorCtrl).Tag as PatientLinkModel;
        }
        private void MazuiMethodCtrl_GotFocus(object sender, RoutedEventArgs e)
        {
            SecheduledListCtrlViewModel.Instance.SelectedP = (sender as AnesMethodCtrl).Tag as PatientLinkModel;
        }

        private void CustomTextCtrl_GotFocus(object sender, RoutedEventArgs e)
        {
            SecheduledListCtrlViewModel.Instance.SelectedP = (sender as CustomComPaiBanBeiZhuCtrl).DataContext as PatientLinkModel;
        }

        private void _selDateTime_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SecheduledListCtrlViewModel.Instance.SelectedP = (sender as CusDatePicker).Tag as PatientLinkModel;
        }

        private void bingRenList1_Drop(object sender, DragEventArgs e)
        {
            try
            {
                if (SecheduleMainCtrlViewModel.Instance.CurrentRoom == null)
                {
                    MessageBoxCommon.ShowDialog("请选择手术间!", MessageBoxCommon.OK);
                    return;
                }
                if (e.Data.GetData("1") != null)
                {
                    IList list = e.Data.GetData("1") as IList;
                    if (list != null)
                    {
                        SecheduleMainCtrlViewModel.Instance.AddSecheduled(list);
                        SecheduleMainCtrlViewModel.Instance.RefUI();
                    }
                }
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void bingRenList1_DragOver(object sender, DragEventArgs e)
        {
            try
            {
                if (e.Data.GetData("1") != null)
                {
                    e.Effects = DragDropEffects.Copy;
                    e.Handled = true;
                }
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void bingRenList1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    checkedList.Clear();
                    List<CheckBox> list = Common.UIHelper.FindVisualChild<CheckBox>(bingRenList1);
                    foreach (var item in list)
                    {
                        if ((bool)item.IsChecked)
                        {
                            checkedList.Add(item.DataContext as PatientLinkModel);
                        }
                    }
                    PaiBan(checkedList);
                }
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void bingRenList1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (bingRenList1.SelectedItems.Count == 0 || SecheduledListCtrlViewModel.Instance.Hospital_code.Equals("SC_CD_TYFSYY")) return;
                DataManager.Instance.CurrentPatientLink = bingRenList1.SelectedItems[0] as PatientLinkModel;
                MainPageCommonViewModel.Instance.NavigateTo("AnesRecordCtrl", false);
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }
        //private void Date_GotFocus(object sender, RoutedEventArgs e)
        //{
        //    CustomTextCtrl haha = sender as CustomTextCtrl;
        //    CusDatePickerWin cusDatePicker = new CusDatePickerWin();
        //    cusDatePicker.SelectedDateTime = Convert.ToDateTime(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + " " + haha.Value + ":00"); 
        //    cusDatePicker.ShowDialog();
        //    if (cusDatePicker.Result)
        //    {
        //        haha.Value = cusDatePicker.SelectedDateTime.ToString("HH:mm");//xxxx年xx月xx日
        //    }
        //}

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                RadioButton rbo = sender as RadioButton;
                if (rbo.Tag == null) return;
                string tag = rbo.Tag.ToString();
                if (tag.Equals("zd"))
                {
                    SecheduleMainCtrlViewModel.Instance.IsZDPT = true;
                }
                else
                {
                    SecheduleMainCtrlViewModel.Instance.IsZDPT = false;
                }
                SecheduledListCtrlViewModel.Instance.ChangeRightCollection();
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }




        #endregion

        private void pbjhsj_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //PatientLinkModel p= (sender as TextBlock).Tag as PatientLinkModel;
            SecheduledListCtrlViewModel.Instance.SelectedP = (sender as TextBlock).Tag as PatientLinkModel;
            if (!SecheduleMainCtrlViewModel.Instance.SelectedSechduledP.Application.Status.Equals(Constants.AP_STATUS_MAZUIQIAN))
            {
                return;
            }
            CusDatePickerWin cusDatePicker = new CusDatePickerWin();
            cusDatePicker.SelectedDateTime = SecheduleMainCtrlViewModel.Instance.SelectedSechduledP.Schedule.Jhsssj;
            cusDatePicker.ShowDialog();
            if (cusDatePicker.Result)
            {
                SecheduleMainCtrlViewModel.Instance.SelectedSechduledP.Schedule.Jhsssj = cusDatePicker.SelectedDateTime;
            }
        }
    }
}
